import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/BlogBean.dart';
import 'package:get/get.dart';

class MenuController extends GetxController {
  ///初始化数据
  void onInit() {
    super.onInit();
    updateBlogList(_currentSelectedIndex.value);
  }

  //菜单选项
  List<String> _menuList = [
    "首页",
    "Flutter",
    "Android",
    "java",
    "IOS",
    "前端",
    "关于我们"
  ];
  GlobalKey<ScaffoldState> _homeScaffoldKey = new GlobalKey();

  //当前显示文章类目的数据
  RxList _blogList = [].obs;

  //当前选中下标
  RxInt _currentSelectedIndex = 0.obs;

  List<String> get menuList => _menuList;

  GlobalKey<ScaffoldState> get homeScaffoldKey => _homeScaffoldKey;

  int get currentSelectedIndex => _currentSelectedIndex.value;

  set currentSelectedIndex(int value) {
    _currentSelectedIndex.value = value;
    updateBlogList(value);
  }

  RxList get blogList => _blogList;

  Map<int, List<BlogBean>> data = new Map();

  Future<void> updateBlogList(int index) async {


    List<BlogBean> list = data[index];

    if (list == null || list.length == 0) {
      list = [];
      String menu = menuList[index];
      //模拟网络请求，延时260ms
      // await Future.delayed(Duration(seconds: 1));
      await Future.delayed(Duration(milliseconds: 260));
      for (int i = 0; i < 10; i++) {
        BlogBean bean = new BlogBean();
        bean.title = "$menu-${i+1}文章说明";
        if (menu.contains("Flutter")) {
          bean.imgPath = "assets/images/flutter.jpg";
        } else if (menu.contains("Android")) {
          bean.imgPath = "assets/images/android.jpg";
        } else if (menu.contains("java")) {
          bean.imgPath = "assets/images/java.jpg";
        } else if (menu.contains("IOS")) {
          bean.imgPath = "assets/images/ios.jpg";
        } else if (menu.contains("前端")) {
          bean.imgPath = "assets/images/web.jpg";
        } else if (menu.contains("关于")) {
          bean.imgPath = "assets/images/about.jpg";
        } else {
          bean.imgPath = "assets/images/home.jpg";
        }
        list.add(bean);
      }
      data[index] = list;
    }
    _blogList.value = list;
  }
}
